import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from "@/views/LoginView.vue";
import AdminView from "@/views/AdminView.vue";
import store from "@/store";
import {Message} from "element-ui";
import WelcomeView from "@/views/WelcomeView.vue";
import UserView from "@/views/UserView.vue";
import DeptView from "@/views/DeptView.vue";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',//运行项目默认进去登陆页面
    name: 'login',
    component: LoginView
  },
  {
    path:'/admin',
    name:'admin',
    component: AdminView,
    children:[
      {
        path:'',
        name:'welcome',
        component:WelcomeView
      },
      {
        path:'user',
        name:'user',
        component: UserView
      },
      {
        path:'dept',
        name:'dept',
        component: DeptView
      }

    ]

  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//设置前置路由守卫进行身份验证
router.beforeEach((to,from,next)=>{
  if(to.name!='login'&& store.state.username===''){
    Message.error('请先登录')
    //并且跳转到登录页面
    next({name:'login'})
  }else{
    next();
  }
})

export default router
